package com.zhentao.service.impl;

import com.zhentao.apis.article.ArticleClientApi;
import com.zhentao.mapper.WmChannelMapper;
import com.zhentao.mapper.WmNewsMapper;
import com.zhentao.mapper.WmUserMapper;
import com.zhentao.model.common.article.dto.ArticleDto;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.wemedia.WmChannel;
import com.zhentao.model.common.wemedia.WmNews;
import com.zhentao.model.common.wemedia.WmUser;
import com.zhentao.service.WmNewsAutoScanService;
import com.zhentao.utils.aliyu.TextAutoRoute;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class WmNewAutoScanServiceImpl implements WmNewsAutoScanService {
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Async  //标明当前方法是一个异步方法
    @Override
    public void autoScanWmNews(Integer id) throws Exception {

        //1.根据文章的id查询到文章的标题+内容然后进行审核
        WmNews wmNews = wmNewsMapper.selectById(id);
        String content = wmNews.getTitle()+ wmNews.getContent();
        //2.进行审核
        ResponseResult result = TextAutoRoute.isGreenText(content);
        if(result.getCode()!=200){
            //审核状态修改成审核失败
            wmNews.setStatus((int) 2);

            wmNews.setReason(result.getErrorMessage()+";"+result.getData());
            wmNewsMapper.updateById(wmNews);
        }else{//审核通过，状态修改为审核成功
            wmNews.setStatus((int) 8);
            wmNewsMapper.updateById(wmNews);
            //如何审核成功，继续进行后续操作

            //调用远程接口进行文章保存
            saveApArticle(wmNews);
        }
    }

    @Autowired
    private ArticleClientApi articleClientApi;
    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmUserMapper wmUserMapper;

    public ResponseResult saveApArticle(WmNews wmNews){
//        try {
//            Thread.sleep(3000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        ArticleDto dto = new ArticleDto();
        //拷贝属性
        BeanUtils.copyProperties(wmNews, dto);
        //文章的布局
        dto.setLayout(wmNews.getType());
        //频道
        WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
        if(wmChannel != null){
            dto.setChannelName(wmChannel.getName());
        }

        //作者
        dto.setAuthorId(wmNews.getUserId());
        WmUser wmUser= wmUserMapper.selectById(wmNews.getUserId());
        if(wmUser != null){
            dto.setAuthorName(wmUser.getName());
        }

        //设置文章id
        if(wmNews.getArticleId() != null){
            dto.setId(wmNews.getArticleId());
        }

        ResponseResult result = articleClientApi.saveArticle(dto);

        //如果发布-保存成功，绑定news的articleId 和状态
        if(result.getCode() == 200){
            wmNews.setStatus(9);//已发布
            wmNews.setArticleId((Long) result.getData()); //绑定文章的id
            wmNews.setReason("审核成功");
            wmNewsMapper.updateById(wmNews);
        }
        return ResponseResult.okResult("发布成功");
    }
}
